var DEFAULT_ELEMENT_NAME = "ids";
var DEFAULT_DELETE_URL = "ajaxdelete/";
var DEFAULT_UPDATE_URL = "preupdate/";

function getIds() {
	return getIds(DEFAULT_ELEMENT_NAME)
};

function getIds(name) {
	var ids = new Array();
	$.each($(":checkbox:checked[name='" + name + "']"), function(i, obj) {
		ids.push($(obj).val());
	});
	return ids;
}

function showDialogue(message) {
	alert(message);
}

function update(url, elementName) {
	if (elementName == null || elementName == undefined
			|| elementName.length == 0) {
		elementName = DEFAULT_ELEMENT_NAME;
	}

	if (url == null || url == undefined || url.length == 0) {
		url = DEFAULT_UPDATE_URL;
	}
	goUpdate(url, elementName);
}

function del(url, elementName) {
	if (elementName == null || elementName == undefined
			|| elementName.length == 0) {
		elementName = DEFAULT_ELEMENT_NAME;
	}

	if (url == null || url == undefined || url.length == 0) {
		url = DEFAULT_DELETE_URL;
	}
	goDelete(url, elementName);
}

function goDelete(url, elementName) {

	var ids = getIds("'" + elementName + "'");
	if (ids.length == 0) {
		showDialogue("请选择一个要删除的条目");
		return;
	} else if (ids.length > 1) {
		if (!confirm("你确定删除多个条目?")) {
			return;
		}
	}

	if (!confirm("确定要删除这个条目?")) {
		return;
	}

	var callback = function() {
		$(
				":checkbox:checked[name='" + elementName + "'][value='"
						+ ids[0] + "']").parent().parent().remove();
	}

	ajaxDelete(url + ids[0], callback);
}

function ajaxDelete(url, callback) {
	var ajaxParams = {
		ssssss : Math.random() * 100000
	};
	$.get(url, ajaxParams, function(data, textStatus, jqXHR) {
		if (textStatus == "success") {
			if (data.error) {
				alert(data.info);
				return false;
			} else if (data.success) {
				callback();
				return true;
			} else {
				alert("出现未知异常.");
				return false;
			}
		}
	}, "json");
}

function goUpdate(url, elementName) {
	var ids = getIds("'" + elementName + "'");
	if (ids.length == 0) {
		showDialogue("请选择一个要修改的事项条目");
		return;
	} else if (ids.length > 1) {
		showDialogue("只能选择一个要修改的事项条目");
		return;
	}
	window.location.href = url + ids[0];
}